package com.yupi.vdada.scoring;

import com.yupi.vdada.common.ErrorCode;
import com.yupi.vdada.exception.BusinessException;
import com.yupi.vdada.exception.ThrowUtils;
import com.yupi.vdada.model.entity.App;
import com.yupi.vdada.model.entity.UserAnswer;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ScoringStrategyExecutor {
    @Resource
    private List<ScoringStrategy> strategies;

    /**
     * 评分
     *
     * @param choiceList
     * @param app
     * @return
     * @throws Exception
     */
    public UserAnswer doScore(List<String> choiceList,  App app) throws Exception {
        Integer appType = app.getAppType();
        Integer appScoringStrategy = app.getScoringStrategy();
        if (appType == null || appScoringStrategy == null) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "应用配置有误，未找到匹配的策略");
        }
        for(ScoringStrategy scoringStrategy:strategies){
            if(scoringStrategy.getClass().isAnnotation()){
                ScoringStrategyConfig annotation = scoringStrategy.getClass().getAnnotation(ScoringStrategyConfig.class);
                if(annotation.appType() == appType && annotation.scoringStrategy() == appScoringStrategy)
                    return scoringStrategy.doScore(choiceList,app);
            }
        }
        throw new BusinessException(ErrorCode.SYSTEM_ERROR, "未找到匹配的策略");
    }
}
